﻿#include "DX3TextureManager.h"

void DX3TextureManager::LoadTexture(ID3D12Device* device, ID3D12GraphicsCommandList* cmdList)
{
    auto bricksTex = std::make_unique<Texture>();
    bricksTex->Name = "bricksTex";
    bricksTex->Filename = L"../../Res/Textures/bricks.dds";
    ThrowIfFailed(DirectX::CreateDDSTextureFromFile12(device,
        cmdList, bricksTex->Filename.c_str(),
        bricksTex->Resource, bricksTex->UploadHeap));

    auto stoneTex = std::make_unique<Texture>();
    stoneTex->Name = "stoneTex";
    stoneTex->Filename = L"../../Res/Textures/stone.dds";
    ThrowIfFailed(DirectX::CreateDDSTextureFromFile12(device,
        cmdList, stoneTex->Filename.c_str(),
        stoneTex->Resource, stoneTex->UploadHeap));

    auto tileTex = std::make_unique<Texture>();
    tileTex->Name = "tileTex";
    tileTex->Filename = L"../../Res/Textures/tile.dds";
    ThrowIfFailed(DirectX::CreateDDSTextureFromFile12(device,
        cmdList, tileTex->Filename.c_str(),
        tileTex->Resource, tileTex->UploadHeap));

    auto crateTex = std::make_unique<Texture>();
    crateTex->Name = "crateTex";
    crateTex->Filename = L"../../Res/Textures/WoodCrate01.dds";
    ThrowIfFailed(DirectX::CreateDDSTextureFromFile12(device,
        cmdList, crateTex->Filename.c_str(),
        crateTex->Resource, crateTex->UploadHeap));

    mTextures[bricksTex->Name] = std::move(bricksTex);
    mTextures[stoneTex->Name] = std::move(stoneTex);
    mTextures[tileTex->Name] = std::move(tileTex);
    mTextures[crateTex->Name] = std::move(crateTex);
}

void DX3TextureManager::BuildDescriptorHeap(ID3D12Device* device, CD3DX12_CPU_DESCRIPTOR_HANDLE hCpuDescriptor,
    UINT cbvSrvDescriptorSize)
{
    auto bricksTex = mTextures["bricksTex"]->Resource;
    auto stoneTex = mTextures["stoneTex"]->Resource;
    auto tileTex = mTextures["tileTex"]->Resource;
    auto crateTex = mTextures["crateTex"]->Resource;

    D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = {};
    srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
    srvDesc.Format = bricksTex->GetDesc().Format;
    srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
    srvDesc.Texture2D.MostDetailedMip = 0;
    srvDesc.Texture2D.MipLevels = bricksTex->GetDesc().MipLevels;
    srvDesc.Texture2D.ResourceMinLODClamp = 0.0f;
    device->CreateShaderResourceView(bricksTex.Get(), &srvDesc, hCpuDescriptor);

    // next descriptor
    hCpuDescriptor.Offset(1, cbvSrvDescriptorSize);

    srvDesc.Format = stoneTex->GetDesc().Format;
    srvDesc.Texture2D.MipLevels = stoneTex->GetDesc().MipLevels;
    device->CreateShaderResourceView(stoneTex.Get(), &srvDesc, hCpuDescriptor);

    // next descriptor
    hCpuDescriptor.Offset(1, cbvSrvDescriptorSize);

    srvDesc.Format = tileTex->GetDesc().Format;
    srvDesc.Texture2D.MipLevels = tileTex->GetDesc().MipLevels;
    device->CreateShaderResourceView(tileTex.Get(), &srvDesc, hCpuDescriptor);

    // next descriptor
    hCpuDescriptor.Offset(1, cbvSrvDescriptorSize);

    srvDesc.Format = crateTex->GetDesc().Format;
    srvDesc.Texture2D.MipLevels = crateTex->GetDesc().MipLevels;
    device->CreateShaderResourceView(crateTex.Get(), &srvDesc, hCpuDescriptor);
}
